﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PublicOfficialSelectionService;

namespace CommunitySystem
{
    public partial class TongZhiDanWindow : ChildWindow
    {
        private PublicOfficialSelectionServiceClient Client = new PublicOfficialSelectionServiceClient();
        private TongZhiModel model = new TongZhiModel();
        public bool IsNew = false;
        public int ID;
        public TongZhiDanWindow(int iD, bool isnew)
        {
            InitializeComponent();
            ID = iD;
            IsNew = isnew;
            for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 10; i--)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = i.ToString();
                cbi.Tag = i;
                nian.Items.Add(cbi);
            }
            if (IsNew)
            {
                this.Title = "新增编制信息";
                model = new TongZhiModel();
                this.DataContext = model;
                nian.SelectedIndex = 0;
            }
            else
            {
                this.Title = "编辑编制信息";
                Client.GetTongZhiModelCompleted += new EventHandler<GetTongZhiModelCompletedEventArgs>(Client_GetTongZhiModelCompleted);
                Client.GetTongZhiModelAsync(0,ID);
            }
            
        }

        void Client_GetTongZhiModelCompleted(object sender, GetTongZhiModelCompletedEventArgs e)
        {
            if (e.Result != null)
                model = e.Result[0];
            this.DataContext = model;
            nian.SelectedIndex = DateTime.Today.Year - model.Year;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(model.Type))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入通知类型！");
                return;
            }
            if (string.IsNullOrEmpty(model.Content))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入通知内容！");
                return;
            }
            if (IsNew == true)
            {
                model.Year = DateTime.Today.Year - nian.SelectedIndex;
                Client.AddTongZhiModelCompleted += new EventHandler<AddTongZhiModelCompletedEventArgs>(Client_AddTongZhiModelCompleted);
                Client.AddTongZhiModelAsync(model);
            }
            else
            {
                model.Year = DateTime.Today.Year - nian.SelectedIndex;
                Client.UpdateTongZhiModelCompleted += new EventHandler<UpdateTongZhiModelCompletedEventArgs>(Client_UpdateTongZhiModelCompleted);
                Client.UpdateTongZhiModelAsync(model);
            }
        }

        void Client_UpdateTongZhiModelCompleted(object sender, UpdateTongZhiModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                this.DialogResult = true;
            }
        }

        void Client_AddTongZhiModelCompleted(object sender, AddTongZhiModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                this.DialogResult = true;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
